www.gusucode.com > 失效有关曲率处理matlab源码程序 > 失效有关曲率处理\Face Segmentation\code.m
clear all alpha=0; rootdir = 'C:\Users\eleqsr\UH10\Academic 2011-12\Student Projects\Face Fuzzy Curvature Based Segmentation\'; %first input directory myFolder = strcat(rootdir, 'photos'); % second input directory myFolder2= strcat(rootdir, 'cropped_images'); numrows = 128; numcols = 128; % reads all the images from directory filePattern = fullfile(myFolder, '*.jpeg'); % reads all the images from directory filePattern2 = fullfile(myFolder2, '*.jpeg'); jpegFiles2 = dir(filePattern2); jpegFiles = dir(filePattern); %counts all the jpeg files in the directory for k = 1:length(jpegFiles) baseFileName = jpegFiles(k).name; fullFileName = fullfile(myFolder, baseFileName); fprintf(1, 'Now reading %s\n', fullFileName); imageArray = imread(fullFileName); % resizing all the images by 128x128 x=imresize(imageArray,[128 128]); %generic co-ordinates for all images to crop different parts of the %face left_eye=imcrop(x,[22 53 35 21]); right_eye=imcrop(x,[73 53 34 20]); nose=imcrop(x,[52 70 28 25]); lips=imcrop(x,[49 95 35 15]); right_cheek=imcrop(x,[82 78 26 16]); left_cheek=imcrop(x,[23 75 25 17]); forehead=imcrop(x,[35 39 58 12]); pathname1 = strcat(rootdir,'cropped images\left eye\'); pathname2 = strcat(rootdir,'cropped images\right eye\'); pathname3 = strcat(rootdir,'cropped images\nose\'); pathname4 = strcat(rootdir,'cropped images\lips\'); pathname5 = strcat(rootdir,'cropped images\right cheek\'); pathname6 = strcat(rootdir,'cropped images\left cheek\'); pathname7 = strcat(rootdir,'cropped images\forehead\'); imwrite(left_eye,[pathname1,'left eye',num2str(k),'.jpeg']); % saves cropped images into the directory imwrite(right_eye,[pathname2,'right eye',num2str(k),'.jpeg']); imwrite(nose,[pathname3,'nose',num2str(k),'.jpeg']); imwrite(lips,[pathname4,'lips',num2str(k),'.jpeg']); imwrite(right_cheek,[pathname5,'right cheek',num2str(k),'.jpeg']); imwrite(left_cheek,[pathname6,'left cheek',num2str(k),'.jpeg']); imwrite(forehead,[pathname7,'forehead',num2str(k),'.jpeg']); end %counts all the jpeg files in the directory for m = 1:length(jpegFiles2) baseFileName = jpegFiles2(m).name; fullFileName = fullfile(myFolder2, baseFileName); fprintf(1, 'Now reading %s\n', fullFileName); imageArray = imread(fullFileName); %resizing the images by 25x40 z=imresize(imageArray,[25 40]); image=rgb2gray(z); l=fspecial('laplacian',0); bwc3=filter2(l,image); [z,y]=size(bwc3); % Four 5x5 bidirectional masks mask1=[0 0 0 0 0;0 1 0 1 0;0 1 0 1 0;0 1 0 1 0;0 0 0 0 0]; mask2=[0 0 1 0 0;0 0 0 1 0;-1 0 0 0 1;0 1 0 1 0;0 0 0 0 0]; mask3=[0 0 0 0 0;0 1 1 1 0;0 0 0 0 0;0 -1 -1 -1 0;0 0 0 0 0]; mask4=[0 0 1 0 0;0 1 0 0 0;1 0 0 0 -1;0 0 0 -1 0;0 0 -1 0 0]; mug1=filter2(mask1,bwc3); mug2=filter2(mask2,bwc3); mug3=filter2(mask3,bwc3); mug4=filter2(mask4,bwc3); for i=1:1:z-4 for j=1:1:y-4 k0(i,j)=mean2(mug1(i:(i+4),j:(j+4))); k45(i,j)=mean2(mug2(i:(i+4),j:(j+4))); k90(i,j)=mean2(mug3(i:(i+4),j:(j+4))); k135(i,j)=mean2(mug4(i:(i+4),j:(j+4))); H(i,j)=0.5*(k0(i,j)+k45(i,j)+k90(i,j)+k135(i,j)); %mean curvature K(i,j)= (k0(i,j)*k90(i,j)+k45(i,j)*k135(i,j))-(H(i,j).^2);% Gaussian curvature end end pathname8 = strcat(rootdir,'\hk\k0\'); pathname9 = strcat(rootdir,'\hk\k45\'); pathname10 = strcat(rootdir,'\hk\k90\'); pathname11 = strcat(rootdir,'\hk\k135\'); pathname12 = strcat(rootdir,'\hk\h\'); pathname13 = strcat(rootdir,'\hk\k\'); imwrite(k0,[pathname8,'k0',num2str(m),'.jpeg']); imwrite(k45,[pathname9,'k45',num2str(m),'.jpeg']); imwrite(k90,[pathname10,'k90',num2str(m),'.jpeg']); imwrite(k135,[pathname11,'k135',num2str(m),'.jpeg']); imwrite(H,[pathname12,'h',num2str(m),'.jpeg']); imwrite(K,[pathname13,'k',num2str(m),'.jpeg']); fis=readfis(strcat(rootdir,'fuzzy\FUZZY.fis')); % reads the implemented fuzzy logic file mfedit(fis); % allows to modify the fis file s=(z-4)*(y-4); HR=reshape(H,[s,1]); KR=reshape(K,[s,1]); [F]=evalfis([HR KR],fis); op=reshape(F,[(z-4),(y-4)]); alpha=alpha+1; % code for defining 25 different surface classification for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (0<=op(i,j) && op(i,j)<=2.5) image0(i,j,1)=80/255; image0(i,j,2)=160/255; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); % subplot(5,5,1);imshow(image0) title('Fuzzy Face Image') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (2.5<=op(i,j) && op(i,j)<=3) image0(i,j,1)=160/255; image0(i,j,2)=1; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,2);imshow(image0) title('B1') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (3<=op(i,j) && op(i,j)<=3.5) image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,3);imshow(image0) title('B3') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (3.5<=op(i,j) && op(i,j)<=4) image0(i,j,1)=80/255; image0(i,j,2)=1; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,4);imshow(image0) title('D1') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (4<=op(i,j) && op(i,j)<=5) image0(i,j,1)=160/255; image0(i,j,2)=1; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,5);imshow(image0) title('R') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (5<=op(i,j) && op(i,j)<=5.4) image0(i,j,1)=80/255; image0(i,j,2)=1; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,6);imshow(image0) title('B6') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (5.4<=op(i,j) && op(i,j)<=5.8) image0(i,j,1)=255; image0(i,j,2)=1; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,7);imshow(image0) title('B8') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (5.8<=op(i,j) && op(i,j)<=6.1) image0(i,j,1)=1; image0(i,j,2)=80/255; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,8);imshow(image0) title('D3') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (6.1<=op(i,j) && op(i,j)<=7.1) image0(i,j,1)=1; image0(i,j,2)=80/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,9);imshow(image0) title('P') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (7.1<=op(i,j) && op(i,j)<=7.5) image0(i,j,1)=80/255; image0(i,j,2)=1; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,10);imshow(image0) title('B11') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (7.5<=op(i,j) && op(i,j)<=8.5) image0(i,j,1)=80/255; image0(i,j,2)=160/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,11);imshow(image0) title('MS') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (8.5<=op(i,j) && op(i,j)<=8.9) image0(i,j,1)=1; image0(i,j,2)=80/255; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,12);imshow(image0) title('B2') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (8.9<=op(i,j) && op(i,j)<=9.2) image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,13);imshow(image0) title('B4') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (9.2<=op(i,j) && op(i,j)<=9.4) image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,14);imshow(image0) title('D2') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (9.4<=op(i,j) && op(i,j)<=10.4) image0(i,j,1)=1; image0(i,j,2)=80/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,15);imshow(image0) title('F') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (10.4<=op(i,j) && op(i,j)<=10.9) image0(i,j,1)=1; image0(i,j,2)=160/255; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,16);imshow(image0) title('B7') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (10.9<=op(i,j) && op(i,j)<=11.2) image0(i,j,1)=1; image0(i,j,2)=80/255; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,17);imshow(image0) title('B9') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (11.2<=op(i,j) && op(i,j)<=11.4) image0(i,j,1)=160/255; image0(i,j,2)=1; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,18);imshow(image0) title('D4') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (11.4<=op(i,j) && op(i,j)<=12.4) image0(i,j,1)=80/255; image0(i,j,2)=80/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,19);imshow(image0) title('SV') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (12.4<=op(i,j) && op(i,j)<=12.8) image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,20);imshow(image0) title('B5') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (12.8<=op(i,j) && op(i,j)<=13.8) image0(i,j,1)=160/255; image0(i,j,2)=80/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,21);imshow(image0) title('VS') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (13.8<=op(i,j) && op(i,j)<=14.1) image0(i,j,1)=160/255; image0(i,j,2)=1; image0(i,j,3)=1; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end figure(alpha); subplot(5,5,22);imshow(image0) title('B10') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (14.1<=op(i,j) && op(i,j)<=15.1) image0(i,j,1)=160/255; image0(i,j,2)=80/255; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end % figure(alpha); % subplot(5,5,23);imshow(image0) % title('PIT') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (15.1<=op(i,j) && op(i,j)<=16.3) image0(i,j,1)=160/255; image0(i,j,2)=160/255; image0(i,j,3)=80/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end % figure(alpha); % subplot(5,5,24);imshow(image0) % title('UN') for i=1:1:z-4 for j=1:1:y-4 image0(i,j)=op(i,j); if (16.3<=op(i,j) && op(i,j)<=16.9) image0(i,j,1)=80/255; image0(i,j,2)=80/255; image0(i,j,3)=160/255; else image0(i,j,1)=1; image0(i,j,2)=1; image0(i,j,3)=1; end end end % figure(alpha); % subplot(5,5,25);imshow(image0) % title('B12') end